package models

import (
	"fmt"
	"log"
	"rongyy/src/app"
)

type SurveyData struct {
	Title  string `json:"title"`
	Answer string `json:"answer"`
}

type SurveyModel struct {
	BaseModel   `xorm:"extends"`
	PatientId   string       `json:"patient_id"`
	PatientName string       `json:"patient_name"`
	Data        []SurveyData `json:"data"`
	Remark      string       `json:"remark"`
	Type        string       `json:"type"`
	AdmNo       string       `json:"adm_no"`
	InpatientNo string       `json:"inpatient_no"`
	CountMy     int          `json:"count_my" xorm:"-"`
	CountYb     int          `json:"count_yb" xorm:"-"`
	CountBmy    int          `json:"count_bmy" xorm:"-"`
}
type ReqSurvey struct {
	PatientId string `json:"patient_id"`
	Limit     int    `json:"limit"`
	Offset    int    `json:"offset"`
}

func (rm *SurveyModel) TableName() string {
	return "survey"
}

func (rm *SurveyModel) Create(req *SurveyModel) error {
	_, err := app.DB().Insert(req)
	if err != nil {
		log.Println(err)
		return err
	}
	return nil
}

func (rm *SurveyModel) GetSurvey(req *ReqSurvey) ([]SurveyModel, int64, error) {
	fmt.Println("req---", req)
	pres := make([]SurveyModel, 0)
	s := app.DB().Table("survey")
	if req.PatientId != "" {
		pid := fmt.Sprintf("%%%s%%", req.PatientId)
		s = s.Where("patient_id like ?", pid)
	}

	total, err := s.Limit(req.Limit, req.Offset).Desc("created_at").FindAndCount(&pres)
	if err != nil {
		return nil, 0, err
	}
	return pres, total, err
}
